<?php 
class SendMsg{
	
	//若实例化的时候传入相应的值则按新的相应值进行
	public function __construct() {
		$this->app_send_account = C('APP_SEND_ACCOUNT');
		$this->app_send_password = C('APP_SEND_PASSWORD');
		$this->app_send_userid = C('APP_SEND_EXTNO');
		$this->url = "http://120.76.202.242:8888/sms.aspx";//推送的地址
	}
	
	/**
	 * 
	 * @param unknown $tel 接收手机号，多个号码间以逗号分隔且最大不超过100个号码
	 * @param unknown $msg 发送内容,标准内容不能超过70个汉字
	 */
	public function sendMsg($tel,$msg){
		$data['userid'] = $this->app_send_userid;
		$data['account'] = $this->app_send_account;
		$data['password'] = $this->app_send_password;
		$data['mobile'] = $tel;
		$data['content'] = $msg;
		$data['sendTime'] = "";//为空表示立即发送
		$data['action'] = "send";
		$data['extno'] = "";
		$result = $this->push_curl($data);
		$result = $this->xml_to_array($result);
		return $result;
	}
	
	/**
	 * 将XML转化为数组
	 * @param unknown $xml
	 * @return multitype:multitype:Ambigous <multitype:>  multitype:Ambigous <multitype:Ambigous <multitype:> , multitype:>  unknown |unknown
	 */
	private function xml_to_array($xml){
		$reg = "/<(\\w+)[^>]*?>([\\x00-\\xFF]*?)<\\/\\1>/";
		if (preg_match_all($reg,$xml,$matches)){
			$count = count($matches[0]);
			$arr = array();
			for($i = 0;$i < $count;$i ++) {
				$key = $matches[1][$i];
				$val = $this->xml_to_array($matches[2][$i]); // 递归
				if (array_key_exists($key,$arr)) {
					if (is_array($arr[$key])){
						if (!array_key_exists(0,$arr[$key])){
							$arr[$key] = array($arr[$key]);
						}
					}else{
						$arr[$key] = array($arr[$key]);
					}
					$arr[$key][] = $val;
				}else{
					$arr[$key] = $val;
				}
			}
			return $arr;
		}else{
			return $xml;
		}
	}
	
	/**
	 * 模拟post进行url请求
	 * @param string $url
	 * @param string $param
	 */
	function push_curl($param = '') {
		if (empty($param)) {
			return false;
		}
		$postUrl = $this->url;
		$curlPost = $param;
		$o = "";
		foreach ($curlPost as $k=>$v ){
			$o.= "$k=". urlencode($v)."&";
		}
		$curlPost = substr($o,0,-1);
		$ch = curl_init();//初始化curl
		curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
		curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
		curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
		curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
		$data = curl_exec($ch);//运行curl
		curl_close($ch);
		return $data;
	}
}

?>